<template>
  <el-form :model="formData" label-width="120px">
    <el-form-item label="姓名">
      <el-input v-model="formData.name" maxlength="30" show-word-limit style="width: 300px"/>
    </el-form-item>
    <el-form-item label="手机号">
      <el-input v-model="formData.phone" maxlength="11" show-word-limit style="width: 300px"/>
    </el-form-item>
    <el-form-item label="身份证号">
      <el-input v-model="formData.id" maxlength="19" show-word-limit style="width: 300px"/>
    </el-form-item>
    <el-form-item label="场所列表">
      <el-select v-model="formData.venueId" class="m-2" placeholder="Select" style="width: 300px">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="门牌号">
      <el-input v-model="formData.address" maxlength="30" show-word-limit style="width: 300px"/>
    </el-form-item>
    <el-form-item style="margin-left: 230px">
      <el-button type="primary" @click="update">修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import {onMounted, ref, reactive} from 'vue'
import {personApi, venueApi} from "@/api/index"
import {useRoute} from 'vue-router'
import {ElMessage} from "element-plus";

const route = useRoute() //获取当前页面的参数
const options = ref([{label: '请选择', value: 0}])
const formData = reactive({
  id: '',
  name: '',
  phone: '',
  address: '',
  venueId: 0
})

onMounted(() => {
  formData.id = String(route.query.id)
  callPersonApi()
  callVenueApi()
})
const callVenueApi = () => {
  venueApi.select.call().then((res: any) => {
    options.value = res.list
    formData.venueId = res.list[0].id
  })
}
const callPersonApi = () => {
  personApi.select.call({id: formData.id}).then((res: any) => {
    let person = res.list[0]
    formData.name = person.name
    formData.id = person.id
    formData.phone = person.phone
    formData.address = person.address
    formData.venueId = person.venueId
  })
}
const update = () => {
  personApi.update.call(formData).then((res: any) => {
    if (res === 1) {
      callPersonApi()
      ElMessage({
        showClose: true,
        message: '修改成功',
        type: 'success'
      })

    } else {
      ElMessage({
        showClose: true,
        message: '修改失败',
        type: 'error'
      })
    }
  })
}
</script>

<style scoped>
</style>
